package io.jee.runshan.controller.admin;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.runshan.domain.Information;
import io.jee.runshan.service.IInformationService;
import io.jee.runshan.utils.Utils;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 消息系统Controller
 * @author SunX
 *
 */
@Controller
@RequestMapping("/admin/information/")
public class InformationController {
	
	private static final String LIST_ACTIVITY = "admin/information/listActivity";
	private static final String LIST_FLOOR = "admin/information/listFloor";
	private static final String ATTR_MALL = "admin/information/attributeMall";
	private static final String ATTR_COMPANY = "admin/information/attributeCompany";
	private static final String ATTR_ACTIVITY = "admin/information/attributeActivity";
	private static final String ATTR_FLOOR = "admin/information/attributeFloor";
	private static final String REDIRECT_LIST = "redirect:/admin/information/list";

	@Resource
	private IInformationService informationService;
	
	/**
	 * 消息列表
	 * @param pager
	 * @param information
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list(Pager<Information> pager, Information information) {
		ConditionGroup group = new ConditionGroup();
		if (!Utils.isNullOrEmpty(information.getTitle())) {
			group.and(Condition.onText("title").like("%" + information.getTitle() + "%"));
		}
		String url = null;
		if (!Utils.isNullOrEmpty(information.getType())) {
			if(Information.TYPE_FLOOR == information.getType()){
				group.and(Condition.onNumber("type").eq(Information.TYPE_FLOOR));
				pager = informationService.list(pager, group, "ASC", "orderIndex");
				url = LIST_FLOOR;
			}else if(Information.TYPE_ACTIVITY == information.getType()){
				group.and(Condition.onNumber("type").eq(Information.TYPE_ACTIVITY));
				pager = informationService.list(pager, group, "DESC", "publishTime");
				url = LIST_ACTIVITY;
			}
		}
		return new ModelAndView(url).addObject("pager", pager).addObject("information", information);
	}
	
	/**
	 * 编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit")
	public ModelAndView edit(Integer id,Integer type) {
		Information information = null;
		String url = null;
		if(!Utils.isNullOrEmpty(type) ){
			if (!Utils.isNullOrEmpty(id)) {
				information = informationService.get(id);
			}else{
				information = new Information();
				information.setType(type);
			}
			if(Information.TYPE_MALL == type){
				url = ATTR_MALL;
				information = informationService.unique("type", type);
			}else if(Information.TYPE_COMPANY == type){
				url = ATTR_COMPANY;
				information = informationService.unique("type", type);
			}else if(Information.TYPE_FLOOR == type){
				url = ATTR_FLOOR;
			}else if(Information.TYPE_ACTIVITY == type){
				url = ATTR_ACTIVITY;
			}
		}
		return new ModelAndView(url).addObject("information", information);
	}
	
	/**
	 * 保存消息
	 * @param information
	 * @return
	 */
	@RequestMapping("save")
	public ModelAndView save(Information information) {
		String msg = "未作任何操作";
		String url = null;
		Integer type = null;
		try {
			if(Information.TYPE_FLOOR == information.getType() || Information.TYPE_ACTIVITY == information.getType()){
				type = information.getType();
				if (Utils.isNullOrEmpty(information.getId())) {
					information.setPublishTime(new Date());
					Integer id = (Integer) informationService.save(information);
					information.setId(id);
					msg = "新增成功！";
				} else {
					Information serInformation = informationService.load(information.getId());
					BeanUtils.copyProperties(information, serInformation,new String[]{"publishTime","type","orderIndex"});
					informationService.update(serInformation);
					msg = "修改成功！";
				}
			}
			if(Information.TYPE_MALL == information.getType() || Information.TYPE_COMPANY == information.getType()){
				type = information.getType();
				Information serInformation = informationService.unique("type", type);
				serInformation.setContent(information.getContent());
				serInformation.setPublishTime(new Date());
				informationService.update(serInformation);
				information = serInformation;
				msg = "修改成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "未知错误！" + e.getMessage();
		}
		if(Information.TYPE_MALL == type){
			url = ATTR_MALL;
		}else if(Information.TYPE_COMPANY == type){
			url = ATTR_COMPANY;
		}else if(Information.TYPE_FLOOR == type){
			url = ATTR_FLOOR;
		}else if(Information.TYPE_ACTIVITY == type){
			url = ATTR_ACTIVITY;
		}
		return new ModelAndView(url).addObject("information", information).addObject("msg", msg);
	}
	
	/**
	 * 删除消息
	 * @param ids
	 * @param type
	 * @param model
	 * @return
	 */
	@RequestMapping("delete/{ids}")
	public String delete(@PathVariable Integer[] ids,Integer type,Model model) {
		String msg = null;
		String url = null;
		if(Information.TYPE_FLOOR == type || Information.TYPE_ACTIVITY == type){
			try {
				if (ids.length > 0) {
					Integer count = informationService.delete("id", ids);
					msg = count + "条记录删除成功！";
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg = "未知错误！" + e.getMessage();
			}
			url = REDIRECT_LIST + "?type=" + type;
		}
		model.addAttribute("msg",msg);
		return url;
	}
	
	/**
	 * 置顶
	 * @param id
	 * @param type
	 * @param model
	 * @return
	 */
	@RequestMapping("/onTop")
	public String onTop(Integer id,Integer type,Model model) {
		String msg = null;
		String url = null;
		if(Information.TYPE_FLOOR == type || Information.TYPE_ACTIVITY == type){
			try {
				if(Information.TYPE_FLOOR == type){
					informationService.updateFloorOnTop(id);
				}else{
					informationService.update("publishTime", new Date(), id);
				}
				msg = "置顶成功！";
			} catch (Exception e) {
				e.printStackTrace();
				msg = "未知错误！" + e.getMessage();
			}
			url = REDIRECT_LIST + "?type=" + type;
		}
		model.addAttribute("msg",msg);
		return url;
	}
}
